import { ref } from 'vue'
import { defineStore } from 'pinia'
import {userLoginAPI} from '@/API/register'
import { showToast } from 'vant'
import { useRouter } from 'vue-router'

export const useUserStore = defineStore('user', () => {
  const info = ref<any>({
    userid:'',
    token:''
  })
  const router = useRouter()
  //登录
const onLogin =async (loginname:any,password:any) => {
  const res:any=await userLoginAPI(loginname, password)
  showToast(res.message)
  if(res.code==='200'){
    info.value.userid = res.data.userid
    info.value.token = res.data.token
   setTimeout(() => router.push('/'),3000)
  }
}

const exitLogin =()=>{
  info.value={}
}

 
 
 
  


  return {
    info,
    onLogin,
    exitLogin
   }
},
{
  persist: true,
})
